//
//  UIUtility.swift
//  shopAssistant
//
//  Created by yuan hong jie on 13/10/2016.
//  Copyright © 2016 com.weitaming. All rights reserved.
//

import Foundation
import UIKit

class UIUtility {
    static func createImageView(_ name: String) -> UIImageView {
        return UIImageView.init(image: UIImage.init(named: name))
    }
    static func createCellRightArrowAtTop(_ top: CGFloat) -> UIImageView {
        let rightArrow = UIUtility.createImageView("shop_right_empty_arrow.png")
        rightArrow.frame = CGRect(x: Const.kPayRightArrowX, y: top, width: Const.kPayRightArrowW, height: Const.kPayRightArrowH)
        return rightArrow
    }
    static func createLabel(_ text: String, fontsizeR: CGFloat) -> UILabel {
        let label = UILabel.init()
        label.text = text
        label.font = UIFont.systemFont(ofSize: 12+fontsizeR)
        return label
    }
    static func createAnnotationImage(_ logoImage: UIImage?) -> UIImage {
        let bgImage = UIImage.init(named:"store_pin_neuatral.png")
        let rect = CGRect(x: 0, y: 0, width: Const.kLogoInMapSize, height: Const.kLogoInMapSize)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        let imgref = UIGraphicsGetCurrentContext()
        imgref!.draw((bgImage?.cgImage!)!, in: rect)
        let rect1 = rect.insetBy(dx: Const.kLogoInMapInsetSize, dy: Const.kLogoInMapInsetSize)
        logoImage?.draw(in: rect1)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result!
    }
    
    static func alert(title t: String, message: String) {
        let alert = UIAlertController.init(title: t, message: message, preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction.init(title: WTMLocalize("common.ok"), style: .cancel, handler: nil))
        UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
    }
    
    static func verifyFormat(password p: String, showAlert: Bool) -> Bool {
        let regex = "^[a-zA-Z0-9]{6,16}$"
        let pred = NSPredicate.init(format: "SELF MATCHES %@", regex)
        if(!pred.evaluate(with: p)) {
            if showAlert {
                self.alert(title: "错误", message: "请输入有效的用户密码")
            }
            return false
        }
        return true
    }
    
    // MARK: one pixel line layer
    static func getOnePixelLineAt(_ top: CGFloat) -> CALayer {
        return self.getOnePixelLine(CGRect(x: 0, y: top, width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL), color: Const.WTM_WHITE_SMOKE) //original 0xECECEC
    }
    
    static func getOnePointLineAt(_ top: CGFloat) -> CALayer {
        return self.getOnePixelLine(CGRect(x: 0, y: top, width: Const.SCREEN_WIDTH, height: 1), color: Const.WTM_WHITE_SMOKE) //original 0xECECEC
    }
    
    fileprivate static func getOnePixelLine(_ frame: CGRect, color: UIColor) -> CALayer {
        let layer = CALayer()
        layer.frame = frame
        layer.backgroundColor = color.cgColor
        return layer
    }
    
    static func calcStringRectSize(_ str: String, font: UIFont, refWidth: CGFloat) -> CGSize {
        let nsstr: NSString = str as NSString
        let textSize = nsstr.boundingRect(with: CGSize(width: refWidth, height: 0),
                                                  options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin, .usesFontLeading],
                                                  attributes: [NSAttributedString.Key.font: font], context:nil)
        return textSize.size
    }
    static func calcStringHeight(_ str: String, font: UIFont, refWidth: CGFloat) -> CGFloat {
        return UIUtility.calcStringRectSize(str, font: font, refWidth: refWidth).height
    }
    static func call(_ number: String) {
        let alert = UIAlertController.init(title: "拨打 \(number)?", message: nil, preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction.init(title: WTMLocalize("common.ok"), style: .default, handler: { (action) in
            UIApplication.shared.openURL(URL(string: number)!)
        }))
        alert.addAction(UIAlertAction.init(title: WTMLocalize("common.cancel"), style: .cancel, handler: nil))
        UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
    }
    
    // MARK: - Image
    static func createImageWithColor(_ color: UIColor) -> UIImage {
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()!
        context.setFillColor(color.cgColor )
        context.fill(rect)
        let theImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return theImage
    }
    
    static func alert(_ txt:String){
        
        let alert = UIAlertView.init(title: "提示", message: txt, delegate: nil, cancelButtonTitle: "确定")
        alert.show()
        
    }
    
    static func isFullScreen() -> Bool{
        if #available(iOS 11, *) {
              guard let w = UIApplication.shared.delegate?.window, let unwrapedWindow = w else {
                  return false
              }
              
              if unwrapedWindow.safeAreaInsets.left > 0 || unwrapedWindow.safeAreaInsets.bottom > 0 {
                  print(unwrapedWindow.safeAreaInsets)
                  return true
              }
        }
        return false
    }
    
    static var kNavigationBarHeight: CGFloat {
       //return UIApplication.shared.statusBarFrame.height == 44 ? 88 : 64
        return isFullScreen() ? 88 : 64
    }

}
